package com.srd.phone.mvp.presenters;

import android.text.TextUtils;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.srd.phone.application.HttpStringRequest;
import com.srd.phone.mvp.interfaces.IBaseView;

import java.util.HashMap;
import java.util.Map;

public class RetrievePwdResetPresenter extends BasePresenter {

    public RetrievePwdResetPresenter(RequestQueue requestQueue, IBaseView iView0) {
        super(requestQueue,iView0);
    }

    //非空验证
    public String checkedPwd(String pwd, String check_pwd) {
        if (TextUtils.isEmpty(pwd)) {
            return "新密码不能为空";
        } else if (TextUtils.isEmpty(check_pwd)) {
            return "确认密码不能为空";
        } else if (!pwd.equals(check_pwd)) {
            return "两次密码不一致";
        }
        return "";
    }

    //网络请求：提交密码
    public void requestSubmit(final String url, final String pwd) {
        HttpStringRequest request = new HttpStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("value", pwd);
                return map;
            }
        };
        request.setTag(url);
        requestQueue.add(request);
    }
}
